package com.zhongkai.web.control.config;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;
import java.util.regex.Pattern;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.log4j.Logger;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import com.zhongkai.model.config.Menu;
import com.zhongkai.service.config.ButtonService;
import com.zhongkai.service.config.MenuService;
import com.zhongkai.tools.CheckWord;

@Controller
public class MenuControl {
	private Logger log=Logger.getLogger(this.getClass());
	private MenuService menuService;
	private ButtonService buttonService;
	
	@RequestMapping("/system/config/menu_add.do")
	public String save(Menu menu,String save_menuSort,HttpServletRequest request,HttpServletResponse response) throws IOException {
		try {
				String menuName=menu.getMenuName();
				if(menuName==null||"".equals(menuName)){
					PrintWriter out=response.getWriter();
					out.write(CheckWord.showMsg("菜单名不能为空!", "/system/config/menu_add_form.jsp",request));
					return null;
				}
				//处理排序输入的非法类型
				Pattern pattern = Pattern.compile("^\\d+$");
				int menuSort;
				if(!pattern.matcher(save_menuSort).matches()){
					PrintWriter out=response.getWriter();
					out.write(CheckWord.showMsg("排序输入类型要正整数!", "/system/config/menu_add_form.jsp",request));
					return null;
				}
				else {
					 menuSort=Integer.parseInt(save_menuSort);
				}
				
				String actionKey=menu.getActionKey();
				if(actionKey==null||"".equals(actionKey)) {
					PrintWriter out=response.getWriter();
					out.write(CheckWord.showMsg("该菜单的actionKey不能为空，请进行更改!", "/system/config/menu_add_form.jsp",request));
					return null;
				}
				boolean isExistActionKey=menuService.isExistActionKey(menu.getActionKey());
				if(!isExistActionKey){
					isExistActionKey=buttonService.isExistActionKey(actionKey);
				}
				if(isExistActionKey){
					PrintWriter out=response.getWriter();
					out.write(CheckWord.showMsg("该菜单的actionKey已经存在，请进行更改!", "/system/config/menu_add_form.jsp",request));
					return null;
				}
				int menuParent=menu.getMenuParent();
				int menuLevel=0;
				if(menuParent!=0){
					menuLevel=menuService.findById(menuParent).getMenuLevel()+1;
				}
				
				menu.setMenuSort(menuSort);
				menu.setMenuLevel(menuLevel);
				menuService.save(menu);	
				return "redirect:/system/config/menu_manage_list.jsp";
		  } catch (Exception e) {
				e.printStackTrace();
				log.error(e.getMessage());
				PrintWriter out=response.getWriter();
				out.write(CheckWord.showMsg("添加菜单时发生异常，请与管理员联系!", "/system/config/menu_add_form.jsp",request));
				return null;
		}
		
	}
	
	@RequestMapping("/system/config/menu_manage_delete.do")
	public String delete(Integer menuId,HttpServletRequest request,HttpServletResponse response) throws IOException {
		try {
			List<Menu> childs=menuService.findChildById(menuId);
			if(childs!=null&&childs.size()>0){
				PrintWriter out=response.getWriter();
				out.write(CheckWord.showMsg("菜单存在子菜单，不能删除!", "/system/config/menu_manage_delete.jsp?menu_id="+menuId,request));
				return null;
			}
			menuService.delete(menuService.findById(menuId));
			return "redirect:/system/config/menu_manage_list.jsp";
		} catch (Exception e) {
			e.printStackTrace();
			log.error(e.getMessage());
			PrintWriter out=response.getWriter();
			out.write(CheckWord.showMsg("删除时发生异常，请与管理员联系!", "/system/config/menu_manage_list.jsp",request));
			return null;
		}
		
		
	}
	
	@RequestMapping("/system/config/menu_manage_modify.do")
	public String update(Menu menu,String update_menuSort,HttpServletRequest request,HttpServletResponse response) throws IOException{
		try {
			String menuName=menu.getMenuName();
			if(menuName==null||"".equals(menuName)){
				PrintWriter out=response.getWriter();
				out.write(CheckWord.showMsg("菜单名不能为空!", "/system/config/menu_manage_modify.jsp?menu_id="+menu.getMenuId(),request));
				return null;
			}
			//处理排序输入的非法类型
			Pattern pattern = Pattern.compile("^\\d+$");
			int menuSort;
			if(!pattern.matcher(update_menuSort).matches()){
				PrintWriter out=response.getWriter();
				out.write(CheckWord.showMsg("排序输入类型要正整数!", "/system/config/menu_manage_modify.jsp?menu_id="+menu.getMenuId(),request));
				return null;
			}
			else {
				 menuSort=Integer.parseInt(update_menuSort);
			}
			
			
			Menu update_menu=menuService.findById(menu.getMenuId());
			String new_actionKey=menu.getActionKey();//新的
			String old_actionKey=update_menu.getActionKey();//旧的
			if(new_actionKey==null||"".equals(new_actionKey)) {
				PrintWriter out=response.getWriter();
				out.write(CheckWord.showMsg("该按钮的actionKey不能为空，请进行更改!", "/system/config/menu_manage_modify.jsp?menu_id="+menu.getMenuId(),request));
				return null;
			}
			if(!new_actionKey.equals(old_actionKey)){
					boolean isExistActionKey=menuService.isExistActionKey(new_actionKey);
					if(!isExistActionKey){
						isExistActionKey=buttonService.isExistActionKey(new_actionKey);
					}
					if(isExistActionKey){
						PrintWriter out=response.getWriter();
						out.write(CheckWord.showMsg("该菜单的actionKey已经存在，请进行更改!", "/system/config/menu_manage_modify.jsp?menu_id="+menu.getMenuId(),request));
						return null;
					}
			}
			update_menu.setActionKey(new_actionKey);
			update_menu.setMenuName(menu.getMenuName());
			update_menu.setMenuSort(menuSort);
			update_menu.setMenuUrl(menu.getMenuUrl());
			menuService.update(update_menu);
			return "redirect:/system/config/menu_manage_list.jsp";
		} catch (Exception e) {
			e.printStackTrace();
			log.error(e.getMessage());
			PrintWriter out=response.getWriter();
			out.write(CheckWord.showMsg("更新时发生异常，请重新试一下!", "/system/config/menu_manage_list.jsp",request));
			return null;
		}
	}

	@Resource
	public void setMenuService(MenuService menuService) {
		this.menuService = menuService;
	}

	@Resource
	public void setButtonService(ButtonService buttonService) {
		this.buttonService = buttonService;
	}

	

}
